#include <QAction>
#include <QMenu>
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "manager_files/window_manager.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , window_manager_(new Window_manager(this))
{
    ui->setupUi(this);

    init_widgets();
    init_connections();

    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init_widgets()
{
    QMenu* file_menu = menuBar()->addMenu(tr("&File"));

    action_exit_ = new QAction(tr("Exit"), this);

    file_menu->addAction(action_exit_);
}

void MainWindow::init_connections()
{
    connect(action_exit_, &QAction::triggered,
            window_manager_, &Window_manager::si_exit_application);

    connect(ui->button_host_address_ip_, &QPushButton::clicked,
            this, &MainWindow::sl_button_host_address_ip);

    connect(this, &MainWindow::si_host_info,
            window_manager_, &Window_manager::si_send_host_info);

    connect(window_manager_, &Window_manager::si_get_host_info,
            this, &MainWindow::sl_get_host_info);
}

void MainWindow::sl_button_host_address_ip()
{
    emit si_host_info(ui->chkbox_only_show_ip4_->isChecked());
}

void MainWindow::sl_get_host_info(QStringList list)
{
    if (0 != list.count()) {
        if ( 0 != ui->plainTextEdit->document()->toPlainText().count()) {
            ui->plainTextEdit->clear();
        }

        for (int i(0); list.count() != i; ++i) {
            ui->plainTextEdit->appendPlainText(list.at(i));
        }
    }
}

